﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;
using System.Data;

namespace Web.Page
{
    public partial class XSHTGL_Query : BasePage
    {
        public const string OP_KEY = "op";
        protected const string CONFIRMED = "1";
        protected const string CMD_CONFIRM = "Confirm";

        protected string id;
        XSHTGL_Dao dao = new XSHTGL_Dao();
        XSHTMXB_Dao daoMXB = new XSHTMXB_Dao();

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            id = Request.QueryString["id"];

            if (!IsPostBack)
            {
                string op = Request.QueryString["op"];
                this.hdOp.Value = op;
                Cache.Insert(OP_KEY, op);

                this.lbTitle.Text = "销售合同列表";
                this.lbMXBTitle.Text = "销售合同明细表";
                this.tbRQStart.Attributes.Add("onclick", "show()");
                this.tbRQEnd.Attributes.Add("onclick", "show()");
                initValue();
                BindGridview();

                Guid jid = new Guid();
                if (id != null && Guid.TryParse(id, out jid))
                {
                    CommandEventArgs cea = new CommandEventArgs("VIEW_MXB", String.Format("{0};{1}", id, 0));
                    ViewMxb(null, cea);
                }
            }
        }

        private void initValue()
        {
            // 销售合同编号

            //List<XSHT> xsbmi = dao.QueryAll();
            //this.ddlbh.DataSource = xsbmi;
            //this.ddlbh.DataTextField = "BH";
            //this.ddlbh.DataValueField = "ID";
            //this.ddlbh.DataBind();

            //合同名称
            //List<XSHT> ktmc = dao.QueryAll();
            //this.ddlktmc.DataSource = ktmc;
            //this.ddlktmc.DataTextField = "KHMC";
            //this.ddlktmc.DataValueField = "ID";
            //this.ddlktmc.DataBind();
            //合同联系人
            List<XSHT> lxr = dao.QueryAll();
            this.ddlhtlxr.DataSource = lxr;
            this.ddlhtlxr.DataTextField = "KHLXR";
            this.ddlhtlxr.DataValueField = "ID";
            this.ddlhtlxr.DataBind();
            this.ddlhtlxr.Items.Insert(0, new ListItem("请选择", "0"));  
            //销售部门ID
            // 部门
            Departments_Dao daoDep1 = new Departments_Dao();
            List<Department> lsDep = daoDep1.QueryAll();
            this.ddlxsbmid.DataSource = lsDep;
            this.ddlxsbmid.DataTextField = "Name";
            this.ddlxsbmid.DataValueField = "ID";
            this.ddlxsbmid.DataBind();
            this.ddlxsbmid.Items.Insert(0, new ListItem("请选择", "0"));  

        }

        private void BindGridview()
        {
            GridView1.PageIndex = RestorePageIndex();
            string bh = this.tbBH.Text.Trim().ToString();
            string ktmc = this.tbMC.Text.Trim().ToString();
            string khlxr = this.ddlhtlxr.SelectedValue.ToString();
            string xsbmid = this.ddlxsbmid.SelectedValue.ToString();
            string xsyid = this.ddlxsyid.SelectedValue.ToString();

            string dhsj = this.tbRQStart.Text.Trim();
            string chsj = this.tbRQEnd.Text.Trim();
            if (!dhsj.Equals(string.Empty) && !chsj.Equals(string.Empty))
            {
                int t = DateTime.Parse(dhsj).CompareTo(DateTime.Parse(chsj));
                if (t > 0)
                {
                    lbResult.Text = "结束时间不能小于开始时间";
                    return;
                }
            }

            List<object> list = dao.QueryBySql2(bh, ktmc, khlxr, xsbmid, xsyid, dhsj, chsj);
            this.GridView1.DataSource = list;
            this.GridView1.DataBind();
        }

        protected void btnMXB_Click(object sender, EventArgs e)
        {

        }

        protected void ViewMxb(object sender, CommandEventArgs e)
        {
            string[] args = e.CommandArgument.ToString().Split(';');
            this.hfSelectedID.Value = args[0];
            BindMXB(args[0]);
            this.hfConfirmed.Value = args[1];
            if (CONFIRMED.Equals(this.hfConfirmed.Value))
            {
                this.btnAddMXB.Visible = false;
            }
            else
            {
                this.btnAddMXB.Visible = true;
            }
            BindMXB(args[0]);
        }

        private void BindMXB(string id)
        {
            //合同明细表
            XSHTMXB_Dao daoCp = new XSHTMXB_Dao();
            List<object> listcp = daoCp.QueryByXSHTMXBEx(id);
            this.gvMXB.DataSource = listcp;
            this.gvMXB.DataBind();
        }

        protected void btnQuery_Click(object sender, ImageClickEventArgs e)
        {
            BindGridview();
        }

        protected void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/Page/XSHTGL_Add.aspx");
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            int result = dao.Delete(id);
            if (result == 1)
            {
                lbResult.Text = "删除成功";
                BindGridview();
            }
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            BindGridview();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindGridview();
            SavePageIndex(e.NewPageIndex);
        }

        //protected class GridViewData
        //{
        //   public Guid ID { get; set; }
        //   public string  BH { get; set; }
        //   public string  KHMC { get; set; }
        //   public string  KHLXR { get; set; }
        //   public Guid XSBMID { get; set; }
        //   public Guid XSRYID { get; set; }
        //   public string  XSBMMC { get; set; }
        //   public string  XSRYMC { get; set; }
        //   public DateTime DHSJ { get; set; }
        //   public DateTime CHSJ { get; set; }
        //   public string CHFS { get; set; }
        //   public int HTZT { get; set; }
        //}

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                string ID= DataBinder.Eval(e.Row.DataItem, "ID").ToString();
                string HTZT = DataBinder.Eval(e.Row.DataItem, "HTZT").ToString();      
               
                Label lblRQ = ((Label)(e.Row.Cells[5].FindControl("lblRQ")));
                LinkButton btnConfirm = ((LinkButton)(e.Row.Cells[9].FindControl("btnConfirm")));
                Button btnViewMXB = ((Button)(e.Row.Cells[10].FindControl("btnViewMXB")));
                btnViewMXB.CommandArgument = String.Format("{0};{1}", ID, HTZT);
                btnConfirm.CommandArgument = ID;

                Label lblZT = ((Label)(e.Row.Cells[9].FindControl("lblZT")));
                if (int.Parse(HTZT) == 0)
                {
                    lblZT.Text = "待确认";
                    lblZT.ForeColor = System.Drawing.Color.Red;
                    btnConfirm.Visible = true;
                }
                else
                {
                    lblZT.ForeColor = System.Drawing.Color.Green;
                    lblZT.Text = "已确认";
                    btnConfirm.Visible = false;
                    btnConfirm.OnClientClick = null;
                }
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)//66
        {
            string id = (string)e.CommandArgument;

            if (e.CommandName.Equals(CMD_CONFIRM))
            {
                Confirm(id);
            }
        }

        protected void Confirm(string id)//66
        {
            XSHT xsht = dao.Query(id);
            xsht.HTZT = 1;
            bool res = false;            
            res = dao.KTState(xsht);           
            if (res)
            {
                lbResult.Text = "确认单据成功。";
                BindGridview();
            }
        }

        protected void gvMXB_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = gvMXB.DataKeys[e.RowIndex].Value.ToString();
            int result = daoMXB.Delete(id);
            if (result == 1)
            {
                lbResult.Text = "删除成功";
                BindMXB(hfSelectedID.Value);
            }
        }

        protected void gvMXB_RowDataBound(object sender, GridViewRowEventArgs e)//66
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton btnDelete = ((LinkButton)(e.Row.Cells[6].FindControl("btnDelete")));

                if (CONFIRMED.Equals(this.hfConfirmed.Value))
                {
                    btnDelete.OnClientClick = null;
                    btnDelete.Enabled = false;
                    // e.Row.Cells[8].Visible = false;
                }

            }
        }



        protected class UserNames
        {
            public string Id { get; set; }
            public string Name { get; set; }
        }

        protected class XSHTMXB_
        {
            public Guid ID { get; set; }
            public Guid XSHTID { get; set; }
            public Guid CPID { get; set; }
            public double SL { get; set; }
            public double DJ { get; set; }
          
        }


        protected void btnAddMXB_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/Page/XSHTGLMIX_Add.aspx?xshtid=" + this.hfSelectedID.Value + "&op=" + hdOp.Value);
        }

        protected void ddlxsbmid_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ddlxsyid.Items.Clear();
            string depId = ddlxsbmid.SelectedValue;
            if (depId != "0")
            {
                Guid guid = Guid.Parse(depId);
                using (var context = new Dao.ErpEntities())
                {
                    var query = from d in context.UserInDepartmentSet
                                join u in context.UserSet
                                on d.UserId equals u.Id
                                where d.DepartmentId == guid
                                select new
                                {
                                    Id = d.UserId,
                                    Name = u.Name
                                };
                    List<UserNames> list = new List<UserNames>();
                    if (query.Count() > 0)
                    {
                        foreach (var q in query)
                        {
                            UserNames un = new UserNames();
                            un.Id = q.Id.ToString();
                            un.Name = q.Name;
                            list.Add(un);
                        }
                    }

                    this.ddlxsyid.DataSource = list;
                    this.ddlxsyid.DataTextField = "Name";
                    this.ddlxsyid.DataValueField = "Id";
                    this.ddlxsyid.DataBind();
                }
            }

            //this.ddlRY.Items.Insert(0, new ListItem("请选择", "0"));
        }



    }
}